package cn.yunhe.controller;
import cn.yunhe.pojo.DeptP;
import cn.yunhe.pojo.GllsResult;
import cn.yunhe.pojo.PageBean;
import cn.yunhe.service.DeptService;
import cn.yunhe.utils.ExportExcelUtil;
import cn.yunhe.vo.DeptVo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public String listDeptAndParent(Model model, PageBean pageBean) {
        PageBean pb = deptService.listDeptOfPage(pageBean);
        pb.setUrl("/dept/list");
        model.addAttribute("pb", pb);
        return "sysadmin/dept/jDeptList";
    }

    @RequestMapping("/tocreate")
    public String tocreate(Model model) {
        List<DeptP> deptList = deptService.listDept();
        model.addAttribute("ds", deptList);
        return "sysadmin/dept/jDeptCreate";
    }

    @RequestMapping("/insert")
    public String insert(DeptP deptP) {
        // deptP 接受  部门名称 和 上级部门 parentId
        // 添加部门
        deptService.addDept(deptP);

        return "redirect:/dept/list";
    }

    /**
     * 查看
     *
     * @return
     */
    @RequestMapping("/toview")
    public String toView(String id, Model model) {

        DeptVo deptVo = deptService.deptSelectByID(id);

        model.addAttribute("dept", deptVo);

        return "sysadmin/dept/jDeptView";
    }

    /**
     * 修改
     *
     * @return
     */
    @RequestMapping("toupdate")
    public String toupdate(String id, Model model, DeptVo dept) {
        DeptVo deptVo = deptService.deptSelectByID(id);
        model.addAttribute("dept", deptVo);
        List<DeptP> deptList = deptService.listDept();
        model.addAttribute("ds", deptList);

        return "sysadmin/dept/jDeptUpdate";
    }

    @RequestMapping("/saveUpdate")
    public String saveUpdate(DeptP deptP) {
        int result = deptService.saveUpdate(deptP);

        return "redirect:/dept/list";
    }


    @RequestMapping("/delete1")
    @ResponseBody
    public GllsResult delete1(String[] deptId) {
        GllsResult result = deptService.deleteDeptByDeptId(deptId);
        return result;
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        deptService.deleteDeptVoById(id);
        return "redirect:/dept/list";
    }


    @RequestMapping("select")
    public String toselect() {
        return "redirect:/dept/list";
    }


    @RequestMapping("export")
    public void export(PageBean pageBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 1. 查询要导出的数据
        PageBean pb = deptService.listDeptOfPage(pageBean);
        List<DeptVo> datas = (List<DeptVo>) pb.getDatas();

        //2.把数据填充到excel 文件中
        ExportExcelUtil excelUtil = new ExportExcelUtil();
        String realPath = request.getServletContext().getRealPath("/");

        String filePath = "/tpl/dept_export.xlsx";
        File excelFile = excelUtil.getExcelTplFile(realPath,filePath);
        //工作簿
        Workbook workbook = excelUtil.getWorkbook(excelFile);
        // 得到  excel 的  sheet 对象
        Sheet sheet = excelUtil.getSheet(workbook, "部门信息");

        for(int i=0;i<datas.size();i++){
            DeptVo deptPVo = datas.get(i);
            // 得到 这个sheet 页的最新一行
            Row row = excelUtil.createRow(sheet);
            Cell cell0 = excelUtil.createCell(row, 0);
            cell0.setCellValue(deptPVo.getDeptNo());

            Cell cell1 = excelUtil.createCell(row, 1);
            cell1.setCellValue(deptPVo.getParentName());
            Cell cell2 = excelUtil.createCell(row, 2);
            cell2.setCellValue(deptPVo.getDeptName());
            Cell cell3 = excelUtil.createCell(row, 3);
            cell3.setCellValue(deptPVo.getState()==1?"启用":"停用");

        }

        //3.把写好数据的 excel 发送到客户端浏览器

        String fileName="dept_list.xlsx";
        response.setContentType("application/ms-excel");
        response.setHeader("Content-disposition", "attachment;filename="+fileName);
        // 通过 response 得到 输出流对象
        ServletOutputStream ouputStream = response.getOutputStream();
        workbook.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();


    }
}
